package org.fuys.coder.domain.auth.service.handler.register;

import lombok.extern.slf4j.Slf4j;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.common.exception.handler.NeedCallBackException;
import org.fuys.coder.infrastructure.model.dto.UserDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.auth.service.handler
 * @className: RegisterHandlerChain
 * @author: WangJing
 * @description: 用户注册处理链路
 * @date: 2024/6/19 18:45
 * @version: 1.0
 */
@Slf4j
public class RegisterHandlerChain {
    private List<RegisterHandler> handlerList=new ArrayList<>();

    public void addHandler(RegisterHandler registerHandler){
        handlerList.add(registerHandler);
    }

    public void removeHandler(RegisterHandler registerHandler){
        handlerList.remove(registerHandler);
    }

    public boolean doHandle(UserDTO userDTO){
        try {
            for (RegisterHandler registerHandler : handlerList) {
                final boolean handle = registerHandler.handle(userDTO);
                //如果只是返回了false 则说明需要中断执行
                if(!handle){
                    return false;
                }
            }
        } catch (BusinessException e) {
            log.error("注册链路中存在异常 信息:"+e.getMsg());
            throw new NeedCallBackException(e.getMsg());
        }
        return true;
    }
}
